﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Phone.Tasks;
using Qplus.protocol;
using Qplus.enums;
using Qplus.Infrastructure;
using System.IO;
using Qplus.beans;
using System.Diagnostics;
using Microsoft.Phone.Scheduler;
using System.ComponentModel;

namespace Qplus.UI
{
    public partial class ChatRoom : PhoneApplicationPage
    {
        byte[] picByte;
        RecordControl recRC, senRC;
        ScrollViewer smileySv = new ScrollViewer() { Height = 300, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto };
        public ChatRoom()
        {
            InitializeComponent();
            smileyInit();
            _loop.Duration = TimeSpan.FromMilliseconds(500d);
            _loop.Completed += new EventHandler(_loop_Completed);
            speakBtn.AddHandler(ButtonEx.MouseLeftButtonDownEvent, new MouseButtonEventHandler(speakBtn_MouseLeftButtonDown), true);
            speakBtn.AddHandler(ButtonEx.MouseLeftButtonUpEvent, new MouseButtonEventHandler(speakBtn_MouseLeftButtonUp), true);
            RoomListener.Instance.receiveMsgEvent += new EventHandler<UIEvent>(Instance_receiveMsgEvent);
            roomName.Text = Room.CurrentRoomInstance.RoomName;
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //MessageBox.Show("123");
            base.OnNavigatedTo(e);
        }
        void Instance_receiveMsgEvent(object sender, UIEvent e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //throw new NotImplementedException();
                ShowMsg(e.Message);
            });
        }
        private void ShowMsg(byte[] msgByte)
        {
            int roomid = msgByte[0] + msgByte[1] * 256 + msgByte[2] * 256 * 256 + msgByte[3] * 256 * 256 * 256;
            E_MESSAGE_TYPE msgtype = (E_MESSAGE_TYPE)msgByte[4];
            int userid = msgByte[5] + msgByte[6] * 256 + msgByte[7] * 256 * 256 + msgByte[8] * 256 * 256 * 256;
            int datetime = msgByte[9] + msgByte[10] * 256 + msgByte[11] * 256 * 256 + msgByte[12] * 256 * 256 * 256;
            string year = System.Text.Encoding.UTF8.GetString(msgByte, 9, 1);
            string month = System.Text.Encoding.UTF8.GetString(msgByte, 10, 1);
            string date = System.Text.Encoding.UTF8.GetString(msgByte, 11, 1);
            string time = System.Text.Encoding.UTF8.GetString(msgByte, 12, 1);
            int datalen = msgByte[13] + msgByte[14] * 256;
            //enum E_MESSAGE_TYPE{EMT_TEXT,EMT_RESURL,EMT_PIC,EMT_PICEX,EMT_VOICEFILE，EMT_NOTIFY  }
            switch (msgtype)// ((E_MESSAGE_TYPE)e.Message[4])
            {
                case E_MESSAGE_TYPE.EMT_TEXT:
                    {
                        RecordControl newRecMsg;
                        string newmsg = System.Text.Encoding.UTF8.GetString(msgByte, 15, msgByte.Length - 15);
                        if(userid==18308) newRecMsg = new RecordControl() { MsgType = msgTypeEnum.sendTxt, Msg = newmsg };
                        else newRecMsg = new RecordControl() { MsgType = msgTypeEnum.receiveTxt, Msg = newmsg };
                        recordWP.Children.Add(newRecMsg);
                        Debug.WriteLine("@@   Receive a Txt Msg: " + newmsg + "  @@");
                        break;
                    }
                case E_MESSAGE_TYPE.EMT_RESURL:
                    {
                        break;
                    }
                case E_MESSAGE_TYPE.EMT_PIC:
                    {
                        Debug.WriteLine("@@   Receive a Pic:  "
                            + System.Threading.Thread.CurrentThread.Name+":"
                            +System.Threading.Thread.CurrentThread.ManagedThreadId +"    @@");
                        //string newmsg = "Receive Pic.";
                        //RecordControl newRecMsg1 = new RecordControl() { MsgType = msgTypeEnum.receiveTxt, Msg = newmsg };
                        //recordWP.Children.Add(newRecMsg1);
                        //break;

                        RecordControl newRecMsg = new RecordControl() { MsgType = msgTypeEnum.receivePic, Pic = ByteArrayToBitmapImage(msgByte, 15, msgByte.Length - 15) };
                        recordWP.Children.Add(newRecMsg);
                        break;
                    }
                case E_MESSAGE_TYPE.EMT_PICEX:
                    {
                        break;
                    }
                case E_MESSAGE_TYPE.EMT_VOICEFILE:
                    {
                        string newmsg = "Receive Voice.";
                        RecordControl newRecMsg1 = new RecordControl() { MsgType = msgTypeEnum.receiveTxt, Msg = newmsg };
                        recordWP.Children.Add(newRecMsg1);
                        break;
                    }
                case E_MESSAGE_TYPE.EMT_NOTIFY:
                    {
                        break;
                    }
                default:
                    break;
            }
            /*BitmapImage recPic = new BitmapImage();
            MemoryStream ms = new MemoryStream();
            ms.Write(msgByte, 15, msgByte.Length - 15);
            recPic.SetSource(ms);
            Image img = new Image();
            img.Source = recPic;
            LayoutRoot.Children.Add(img);*/
        }
        public static byte[] ImageToByteArray(BitmapImage imageSource)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                WriteableBitmap btmMap = new WriteableBitmap(imageSource);
                // write an image into the stream
                Extensions.SaveJpeg(btmMap, ms,
                    imageSource.PixelWidth, imageSource.PixelHeight, 0, 100);
                return ms.ToArray();
            }
        }
        public static BitmapImage ByteArrayToBitmapImage(byte[] bits)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream ms = new MemoryStream(bits))
            {
                bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
                bitmapImage.SetSource(ms);
                return bitmapImage;
                //Image image = new Image();
                //image.Source = bitmapImage;
                //return image;
            }
        }
        public static BitmapImage ByteArrayToBitmapImage(byte[] bits, int index, int length)
        {
            BitmapImage bitmapImage = new BitmapImage();
            MemoryStream ms = new MemoryStream();
            ms.Write(bits, index, length);
            bitmapImage.SetSource(ms);
            return bitmapImage;
            //Image img = new Image();
            //img.Source = bitmapImage;
            //return img;
        }
        /// <summary>
        /// 获取缩略图
        /// </summary>
        /// <param name="source">原图像流</param>
        /// <param name="width">缩略图宽</param>
        /// <param name="height">缩略图高</param>
        /// <returns>缩略图流</returns>
        public static Stream GetThumbImage(Stream source, int width, int height)
        {
            if (source == null)
                return null;

            Stream target = new MemoryStream();
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(source);
            WriteableBitmap wb = new WriteableBitmap(bitmap);
            wb.SaveJpeg(target, width, height, 0, 75);
            return target;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage bitmapImage1 = new BitmapImage(new Uri("/SplashScreenImage.jpg", UriKind.RelativeOrAbsolute));
            bitmapImage1.CreateOptions = BitmapCreateOptions.None;
            byte[] data = ImageToByteArray(bitmapImage1);
            Image image1 = new Image();
            image1.Source=ByteArrayToBitmapImage(data);
            //stackPanel1.Children.Add(image1);
        }
        //friendInfo
        private void friendInfoBtnEx_Click(object sender, RoutedEventArgs e)
        {
            PopupContainer pc = new PopupContainer(this);
            PopupUserInfo popupUserInfo = new PopupUserInfo() { IsFriend = true };
            //popupUserInfo.starChatBtnEx.Click += starChatBtnEx_Click;
            //popupUserInfo.starChatBtnEx.Visibility = System.Windows.Visibility.Visible;
            pc.Show(popupUserInfo);
        }
        //Records
        private void chatRecordsListBox_Update()
        {
        }
        //img select
        private void selectBtn_Click(object sender, RoutedEventArgs e)
        {
            if (popupGrid.Visibility == System.Windows.Visibility.Collapsed)
            {
                popupGrid.Visibility = System.Windows.Visibility.Visible;
            }
            else if (popupGrid.Visibility == System.Windows.Visibility.Visible)
            {
                popupGrid.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        private void smileyInit()
        {
            WrapPanel smileyWP = new WrapPanel() { MaxWidth = 480 };
            for (int i = 0; i < 99; i++)
            {
                SmileyControl smCtrl = new SmileyControl() { Editable = false, Index = i + 1, Margin = new Thickness(2) };
                smileyWP.Children.Add(smCtrl);
            }
            smileySv.Content = smileyWP;
        }
        private void smileyBtnEx_Click(object sender, RoutedEventArgs e)
        {
            popupGrid.Visibility = System.Windows.Visibility.Collapsed;
            PopupContainer pc = new PopupContainer(this);
            pc.Show(smileySv);
        }
        private void picBtnEx_Click(object sender, RoutedEventArgs e)
        {
            //ResourceIntensiveTask ritask = new ResourceIntensiveTask("");
            //PeriodicTask ptask = new PeriodicTask("");
            //BackgroundWorker bw = new BackgroundWorker();
            //bw.DoWork += BackgroundWork;
            //bw.RunWorkerCompleted += (x, xe) =>
            //{
            //    // This is called on the UI thread 
            //    KeepAlive.getInstance().startKeepAlive();
            //};
            //bw.RunWorkerAsync();

            popupGrid.Visibility = System.Windows.Visibility.Collapsed;
            PhotoChooserTask picCTask = new PhotoChooserTask();
            picCTask.Show();
            picCTask.Completed += new EventHandler<PhotoResult>(picCTask_Completed);
        }
        private void BackgroundWork(object sender, DoWorkEventArgs e)
        {
            // This is where your background magic happens.
        }

        void picCTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                Debug.WriteLine("###################################");
                QplusT.Instance = new QplusT();
                QplusT.Instance.relog();
                LoginLogout.Instance.loginEvent += new EventHandler<UIEvent>(Instance_loginEvent);

                MemoryStream ms = new MemoryStream();
                ms = (MemoryStream)GetThumbImage(e.ChosenPhoto, 100, 100);
                int len = (int)ms.Length;
                picByte = ms.GetBuffer();
                //byte[] picByte = (e.ChosenPhoto as MemoryStream).GetBuffer();
                Debug.WriteLine("###################################");
                #region
                BitmapImage bmpSource = new BitmapImage();
                bmpSource.SetSource(e.ChosenPhoto);
                //添加到聊天记录中
                TextBlock timeTB = new TextBlock()
                {
                    Text = DateTime.Now.ToString(),
                    Foreground = new SolidColorBrush(Colors.Gray),
                    TextAlignment = TextAlignment.Center,
                    Width = 480,
                };
                senRC = new RecordControl()
                {
                    MsgType = msgTypeEnum.sendPic,
                    Pic = bmpSource,
                };
                recordWP.Children.Add(timeTB);
                recordWP.Children.Add(senRC);
                #endregion
            }
            else
            {
                //do nothing
            }
        }

        void Instance_loginEvent(object sender, UIEvent e)
        {
            //throw new NotImplementedException();
            ProtocolCommand.Instance.pcmd_C2U_REQ_ENTER_ROOM(Room.CurrentRoomInstance.RoomID);
            RoomListener.Instance.enterEvent += new EventHandler<UIEvent>(Instance_enterEvent);
        }

        void Instance_enterEvent(object sender, UIEvent e)
        {
            //throw new NotImplementedException();
            ProtocolCommand.Instance.pcmd_C2U_REQ_ROOM_SENDMSG(E_MESSAGE_TYPE.EMT_PIC, picByte);
        }
        private void cameraBtnEx_Click(object sender, RoutedEventArgs e)
        {
            popupGrid.Visibility = System.Windows.Visibility.Collapsed;
            CameraCaptureTask camCTask = new CameraCaptureTask();
            camCTask.Show();
            camCTask.Completed += new EventHandler<PhotoResult>(camCTask_Completed);
        }
        void camCTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                int len = (int)e.ChosenPhoto.Length;
                byte[] picbyte = new byte[len];
                e.ChosenPhoto.Read(picbyte, 0, len);
                ProtocolCommand.Instance.pcmd_C2U_REQ_ROOM_SENDMSG(E_MESSAGE_TYPE.EMT_PIC, picbyte);
                #region
                BitmapImage bmpSource = new BitmapImage();
                bmpSource.SetSource(e.ChosenPhoto);
                //添加到聊天记录中
                TextBlock timeTB = new TextBlock()
                {
                    Text = DateTime.Now.ToString(),
                    Foreground = new SolidColorBrush(Colors.Gray),
                    TextAlignment = TextAlignment.Center,
                    Width = 480,
                };
                recRC = new RecordControl()
                {
                    MsgType = msgTypeEnum.receivePic,
                    Pic = bmpSource,
                };
                senRC = new RecordControl()
                {
                    MsgType = msgTypeEnum.sendPic,
                    Pic = bmpSource,
                };
                recordWP.Children.Add(timeTB);
                recordWP.Children.Add(recRC);
                recordWP.Children.Add(senRC);
                #endregion
            }
            else
            {
                //do nothing
            }
        }
        //switch msg voice
        private void msgModeBtn_Click(object sender, RoutedEventArgs e)
        {
            msgModeBtn.Visibility = System.Windows.Visibility.Collapsed;
            speakBtn.Visibility = System.Windows.Visibility.Collapsed;
            inputSV.Visibility = System.Windows.Visibility.Visible;
            //inputRTC.Visibility = System.Windows.Visibility.Visible;
            sendMsgBtn.Visibility = System.Windows.Visibility.Visible;
            voiceModeBtn.Visibility = System.Windows.Visibility.Visible;
        }
        private void voiceModeBtn_Click(object sender, RoutedEventArgs e)
        {
            voiceModeBtn.Visibility = System.Windows.Visibility.Collapsed;
            inputSV.Visibility = System.Windows.Visibility.Collapsed;
            //inputRTC.Visibility = System.Windows.Visibility.Collapsed;
            sendMsgBtn.Visibility = System.Windows.Visibility.Collapsed;
            msgModeBtn.Visibility = System.Windows.Visibility.Visible;
            speakBtn.Visibility = System.Windows.Visibility.Visible;
        }
        //msg
        private void sendMsgBtn_Click(object sender, RoutedEventArgs e)
        {
            if ("" != inputTBox.Text)
            {
                byte[] msg = System.Text.Encoding.UTF8.GetBytes(inputTBox.Text);
                ProtocolCommand.Instance.pcmd_C2U_REQ_ROOM_SENDMSG(E_MESSAGE_TYPE.EMT_TEXT, msg);

                RecordControl newSenMsg = new RecordControl() { MsgType = msgTypeEnum.sendTxt, Msg = inputTBox.Text };
                recordWP.Children.Add(newSenMsg);

                inputTBox.Text = "";
            }
#region TEST
            //byte[] testB=new byte[5];
            //for (int i=0;i<5;i++)testB[i]= Convert.ToByte('a'+ i);
            //protocol.C2U_REQ_ROOM_SENDMSG command = new protocol.C2U_REQ_ROOM_SENDMSG();
            //command.sendMessage(14,0,testB,100);//(long roomID, int messageType, byte[] data, int dlen)

            //var var = inputRTC.richTxtCtrlWP.Children.ElementAt(0);
            //if (inputRTC.richTxtCtrlWP.Children.Count > 0 || (var as TextBox).Text != "")
            //{
            //    //RecordControl newRecMsg = new RecordControl() { MsgType = msgTypeEnum.receiveTxt };
            //    //RecordControl newSenMsg = new RecordControl() { MsgType = msgTypeEnum.sendTxt };
            //    //inputRTC.contentCopy(newRecMsg.recRTC);
            //    //inputRTC.contentCopy(newSenMsg.senRTC);
            //    //recordWP.Children.Add(newRecMsg);
            //    //recordWP.Children.Add(newSenMsg);

            //    //TextBox tmp2 = new TextBox() { Text = "顶替", Width = 100, Foreground = new SolidColorBrush(Colors.Black), Style = (Style)App.Current.Resources["noBorderTB"] };
            //    //this.inputRTC.richTxtCtrlWP.Children.Add(tmp2);
            //    //testWP = newRecMsg.recRTC.richTxtCtrlWP;
            //    //TextBlock tmptb = new TextBlock() { Text = "123", Foreground = new SolidColorBrush( Colors.Red)};
            //    //testWP.Children.Add(tmptb);
            //}
            ////TextBox
            ////bool isMsgCanSend = false;
            //if (inputTBox.Text != "")
            //{
            //    TextBlock timeTB = new TextBlock()
            //    {
            //        Text = DateTime.Now.ToString(),
            //        Foreground = new SolidColorBrush(Colors.Gray),
            //        TextAlignment = TextAlignment.Center,
            //        Width = 480,
            //        //HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
            //    };
            //    RecordControl recRC = new RecordControl()
            //    {
            //        MsgType = msgTypeEnum.receiveTxt,
            //        Msg = inputTBox.Text,

            //    };
            //    RecordControl senRC = new RecordControl()
            //    {
            //        MsgType = msgTypeEnum.sendTxt,
            //        Msg = inputTBox.Text,

            //    };
            //    recordWP.Children.Add(timeTB);
            //    recordWP.Children.Add(recRC);
            //    recordWP.Children.Add(senRC);
            //    inputTBox.Text = "";
            //}
#endregion
        }

        //voice
        private int talkNum = 0;
        DateTime starTime, endTime;
        private Storyboard _loop = new Storyboard();
        void _loop_Completed(object sender, EventArgs e)
        {
            if (talkNum % 2 == 0)
            {
                talk_ring1.Visibility = System.Windows.Visibility.Visible;
                talk_ring2.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                talk_ring1.Visibility = System.Windows.Visibility.Collapsed;
                talk_ring2.Visibility = System.Windows.Visibility.Visible;
            }
            talkNum++;
            _loop.Begin();
        }
        private void speakBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //开始录音
            starTime = DateTime.Now;
            //开始播放录音动画
            talkringGrid.Visibility = System.Windows.Visibility.Visible;
            _loop.Begin();
        }
        private void speakBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //录音结束
            endTime = DateTime.Now;

            //停止播放录音动画
            talk_ring1.Visibility = System.Windows.Visibility.Collapsed;
            talk_ring2.Visibility = System.Windows.Visibility.Collapsed;
            talkringGrid.Visibility = System.Windows.Visibility.Collapsed;
            _loop.Stop();
            talkNum = 0;

            //添加到聊天记录中
            TextBlock timeTB = new TextBlock()
            {
                Text = DateTime.Now.ToString(),
                Foreground = new SolidColorBrush(Colors.Gray),
                TextAlignment = TextAlignment.Center,
                Width = 480,
                //HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
            };
            //Grid timeGrid = new Grid();
            //timeGrid.Children.Add(timeTB);
            recRC = new RecordControl()
            {
                MsgType = msgTypeEnum.receiveVoice,
                Msg = (endTime - starTime).Minutes.ToString() + '\'' + (endTime - starTime).Seconds.ToString() + '\"',
                VoiceLength = (int)(endTime - starTime).TotalSeconds * 15 + 80,
            };
            senRC = new RecordControl()
            {
                MsgType = msgTypeEnum.sendVoice,
                Msg = (endTime - starTime).Minutes.ToString() + '\'' + (endTime - starTime).Seconds.ToString() + '\"',
                VoiceLength = (int)(endTime - starTime).TotalSeconds * 15 + 80,
            };
            //senRC.sendVoice.Width = recRC.sendVoiceTxt.ActualWidth + 30;
            recordWP.Children.Add(timeTB);
            recordWP.Children.Add(recRC);
            recordWP.Children.Add(senRC);
            //recordSV.ScrollToVerticalOffset(recordWP.ActualHeight);
            if (recordSV.ViewportHeight + recordSV.VerticalOffset > recordSV.ExtentHeight - recordSV.ViewportHeight * 3)
            {
                //button1_Click(null, null);
            }
        }
        //goto bottom
        private void recordWP_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            recordSV.ScrollToVerticalOffset(recordWP.ActualHeight);
        }

        #region TEST CODE
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            inputRTC.firstTxt.Text = inputRTC.ActualWidth.ToString();
            //inputRTC.firstTxt.Text = content.ActualHeight.ToString();
            //inputRTC.firstTxt.Text = inputRTC.ActualHeight.ToString();
            //recordSV.ScrollToVerticalOffset(recordSV.ExtentHeight);
            /*
            TextBlock testTB = new TextBlock()
            {
                Foreground = new SolidColorBrush(Colors.Black),
                Text = "receive:" + recRC.receiveGrid.ActualWidth.ToString() + ",send:" + senRC.sendGrid.ActualWidth.ToString()
            };
            recordWP.Children.Add(testTB);
             * */
        }

        private void NOTUSE_addSmiley(Grid smileyGrid)
        {
            for (int i = 0; i < 9; i++)
            {
                ColumnDefinition colDef = new ColumnDefinition();
                smileyGrid.ColumnDefinitions.Add(colDef);
            }
            for (int i = 0; i < 10; i++)
            {
                RowDefinition rowDef = new RowDefinition();
                smileyGrid.RowDefinitions.Add(rowDef);
            }
            for (int i = 0; i < 99; i++)
            {
                SmileyControl smCtrl = new SmileyControl() { Index = i + 1 };
                Grid.SetRow(smCtrl, i / 9);
                Grid.SetColumn(smCtrl, i % 9);
                smileyGrid.Children.Add(smCtrl);
                /*
                Border smileyBg = new Border() { Background = head.Background, Visibility = System.Windows.Visibility.Collapsed};
                Image smileyImg = new Image()
                {
                    Source = new BitmapImage(new Uri("/Qplus;component/Pic/smiley_" + (i + 1).ToString() + ".png", UriKind.RelativeOrAbsolute)),
                    Stretch = Stretch.None
                };
                ButtonEx smileyPicBtnEx = new ButtonEx()
                {
                    Background = new SolidColorBrush(Colors.Red),
                    PressedBackground = new SolidColorBrush(Colors.Blue),
                };
                smileyBtnEx.Click += smileyPicBtnEx_Click;
                Grid.SetRow(smileyBg, i / 9);
                Grid.SetColumn(smileyBg, i % 9);
                Grid.SetRow(smileyImg, i / 9);
                Grid.SetColumn(smileyImg, i % 9);
                Grid.SetRow(smileyPicBtnEx, i / 9);
                Grid.SetColumn(smileyPicBtnEx, i % 9);
                smileyGrid.Children.Add(smileyBg);
                smileyGrid.Children.Add(smileyImg);
                smileyGrid.Children.Add(smileyPicBtnEx);*/
            }
        }

        #endregion
        private void inputTBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox inputTB = sender as TextBox;
            int curPosition = inputTB.SelectionStart;
            ScrollViewer sv = inputTBox.Parent as ScrollViewer;
            double svPsition = 0;
            if (inputTB.Text.Length > 0)
            {
                svPsition = (double)curPosition / (double)inputTB.Text.Length * inputTB.ActualWidth;
            }
            if (svPsition != 0)
            {
                sv.ScrollToHorizontalOffset(svPsition);
            }
            else
            {
                sv.ScrollToHorizontalOffset(0);
            }
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ProtocolCommand.Instance.pcmd_C2U_REQ_LEAVE_ROOM(Room.CurrentRoomInstance.RoomID);
        }
    }
}